package com.zmm.util.generate.number;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author 900045
 * @description:
 * @name OrderSnUtil
 * @date By 2021-06-22 11:27:52
 */
public final class OrderSnUtil {

	public static String genOrderSn(Long userId, String prefix) {
		String sn = genSn(userId);
		return prefix.concat(sn);
	}

	private static String genSn(Long userId) {
		if (userId == null) {
			return null;
		}
		String id = CodeUtils.formatSign(userId);
		String[] ids = id.split("");
		String times = Long.toString(System.currentTimeMillis());
		times = times.substring(1);
		String prefix = times.substring(0, CodeUtils.SIGN_LENGTH);
		String suffix = times.substring(CodeUtils.SIGN_LENGTH);
		String[] prefixes = prefix.split("");
		List<String> s = Arrays.asList(prefixes);
		Collections.reverse(s);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < CodeUtils.SIGN_LENGTH; i++) {
			sb.append(s.get(i)).append(ids[i]);
		}
		String code = sb.toString();
		sb = new StringBuffer();
		sb.append(code.substring(0, CodeUtils.SIGN_LENGTH))
				.append(suffix.substring(0, CodeUtils.SIGN_LENGTH / 2))
				.append(code.substring(CodeUtils.SIGN_LENGTH))
				.append(suffix.substring(CodeUtils.SIGN_LENGTH / 2));
		return sb.toString();
	}
}
